package com.minova.demo.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import com.minova.demo.entity.TzBasket;
import com.minova.demo.entity.TzOrder;
import com.minova.demo.entity.TzOrderItem;
import com.minova.demo.service.ITzBasketService;
import com.minova.demo.service.ITzOrderItemService;
import com.minova.demo.service.ITzOrderService;
import com.minova.demo.service.ITzSkuService;
import com.minova.demo.service.MergeService;

@Service
public class MergeServiceImpl implements MergeService {

	@Autowired
	private ITzBasketService basketService;
	
	@Autowired
	private ITzSkuService skuService;
	
	@Autowired
	private ITzOrderItemService itemService;
	
	@Autowired
	private ITzOrderService orderService;
	
	@Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = Exception.class)
	@Override
	public boolean saveOrder(List<TzOrder> orderlist, List<Long> ids, List<TzBasket> baskets) {
		boolean flag = true;
		try {
		List<TzBasket> basketList = baskets;
		for (TzOrder tzOrder : orderlist) {
			//设置订单对象属性
			String flow = orderService.flow();
			tzOrder.setOrderNumber(flow);
			tzOrder.setDvyFlowId(flow);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			String date = df.format(new Date());// new Date()为获取当前系统时间
			tzOrder.setCreateTime(LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			tzOrder.setPayTime(LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			tzOrder.setUpdateTime(LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			tzOrder.setIsPayed(true);
			System.out.println(tzOrder.getCreateTime());
			//添加订单表
			orderService.save(tzOrder);
			basketList = addOrderitem(basketList,tzOrder);
		}
		
		
			//int num = 0 / 0;
			
		} catch (Exception e) {
			throw e;		
		}
		//orderService.removeByIds(basketIds);
		return flag;
	}

	private List<TzBasket> addOrderitem(List<TzBasket> basketList, TzOrder tzOrder) {

		List<TzBasket> baskets = new ArrayList<>();
		baskets.addAll(basketList);
		for (TzBasket tzBasket : basketList) {
			
			if(!tzOrder.getShopId().equals(tzBasket.getTzShop().getShopId())) {
				continue;
			}
			
			//向订单详细对象 属性添加值
			System.out.println(tzBasket.getTzProd().getProdName());
			TzOrderItem orderItem = new TzOrderItem();
			orderItem.setShopId(tzBasket.getTzShop().getShopId());
			orderItem.setOrderNumber(tzOrder.getOrderNumber());
			orderItem.setProdId(tzBasket.getTzProd().getProdId());
			orderItem.setSkuId(tzBasket.getTzSku().getSkuId());
			orderItem.setProdCount(tzBasket.getBasketCount());
			orderItem.setProdName(tzBasket.getTzProd().getProdName());
			orderItem.setSkuName(tzBasket.getTzSku().getSkuName());
			orderItem.setPic(tzBasket.getTzProd().getPic());
			orderItem.setPrice(tzBasket.getTzProd().getPrice());
			orderItem.setUserId(tzBasket.getTzUser().getUserId());
			BigDecimal count = new BigDecimal(tzBasket.getBasketCount()+"");
			BigDecimal price =tzBasket.getTzProd().getPrice();
			orderItem.setProductTotalAmount(count.multiply(price));
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			String date = df.format(new Date());// new Date()为获取当前系统时间
			orderItem.setRecTime(LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			orderItem.setCommSts(0);
			//添加订单详细
			itemService.save(orderItem);
			//删除购物车
			baskets.remove(tzBasket);
			//付款之后修改商品的实际数量
			skuService.updateSkuActualStocks(tzBasket);
			
			basketService.removeById(tzBasket.getBasketId());
		}
		return baskets;
	}

	@Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = Exception.class)
	@Override
	public void updateBasketBasketCount(TzBasket tzBaskets) {
		TzBasket basket = basketService.getById(tzBaskets);
		basketService.saveBasket(tzBaskets);
		if(tzBaskets.getBasketCount()<basket.getBasketCount()) {
			skuService.skuStocksAdd(basket.getTzSku().getSkuId());
		}else {
			skuService.skuStocksDecrease(basket.getTzSku().getSkuId());
		}
	}

	@Transactional(propagation = Propagation.NESTED, isolation = Isolation.DEFAULT, readOnly = false, rollbackFor = Exception.class)
	@Override
	public void removeBasket(List<TzBasket> baskets) {
		// TODO Auto-generated method stub
		for (TzBasket tzBasket : baskets) {
			skuService.updateSkuStock(tzBasket);
			basketService.removeById(tzBasket.getBasketId());
			
		}
	}

}
